package com.scau.ums.controller;

import com.scau.ums.mapper.CustomedFillRepairResult_Material;
import com.scau.ums.mapper.RepairtaskorderMapper;
import com.scau.ums.model.Repairmaterial;
import com.scau.ums.model.Repairresult;
import com.scau.ums.model.Repairtaskorder;
import com.scau.ums.service.imp.RepairReport.FillRepairResult_MaterialImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class FillRepairResult_MaterialController {
    @Autowired
    private FillRepairResult_MaterialImp fillRepairResult_materialImp;
    @Autowired
    private RepairtaskorderMapper repairTaskOrderMapper;
    @Autowired
    private CustomedFillRepairResult_Material customedFillRepairResult_Material;

    @RequestMapping(value = "RepairMaterial")
    public String RepairMaterial(){
        return "RepairMaterial";
    }

    @RequestMapping(value="RepairResult")
    public String RepairResult(){
        return "RepairResult";
    }

    @RequestMapping(value="EnterFillRepairResult")
    public String EnterFillRepairResult(HttpServletRequest request,int repairOrderId){
        Repairtaskorder repairTaskOrder = repairTaskOrderMapper.selectByPrimaryKey(repairOrderId);
        String maintainerName = repairTaskOrder.getMaintainername();
        request.setAttribute("maintainerName",maintainerName);
        request.setAttribute("repairTaskOrder",repairTaskOrder);
        return "FillRepairResult";
    }

    @RequestMapping(value = "EnterFillRepairMaterial")
    public String EnterFillRepairMaterial(HttpServletRequest request,int repairOrderId){
        Repairtaskorder repairTaskOrder = repairTaskOrderMapper.selectByPrimaryKey(repairOrderId);
        request.setAttribute("repairTaskOrder",repairTaskOrder);
        return "FillRepairMaterial";
    }

    @RequestMapping(value="FillRepairResult")
    @ResponseBody
    public Repairresult FillRepairResult(@RequestBody Repairresult repairResult){
        fillRepairResult_materialImp.FillRepairResult(repairResult);
        return repairResult;
    }

    @RequestMapping(value="FillRepairMaterial")
    @ResponseBody
    public Repairmaterial FillRepairMaterial(@RequestBody Repairmaterial repairMaterial){
        fillRepairResult_materialImp.FillRepairMaterial(repairMaterial);
        return repairMaterial;
    }

    @RequestMapping("CheckRepairResultExistence")
    @ResponseBody
    public String CheckRepairResultExistence(int repairOrderId){
        if(fillRepairResult_materialImp.CheckRepairResultExistence(repairOrderId)!=null){
            return "Exist";
        }
        else{
            return "NotExist";
        }
    }

    @RequestMapping("rewriteRepairResult")
    @ResponseBody
    public Repairresult rewriteRepairResult(@RequestBody Repairresult repairResult){
        fillRepairResult_materialImp.rewriteRepairResult(repairResult);
        return repairResult;
    }

    @RequestMapping("/getTaskMaterialList")
    @ResponseBody
    public Map<String, Object> getTaskMaterialList(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        int curPage = Integer.parseInt(request.getParameter("curPage"));
        String searchKeyword = request.getParameter("search");
        if(searchKeyword == null)
            searchKeyword = "";
        int totalRecord = customedFillRepairResult_Material.getMaterialNumber(searchKeyword);

        map.put("totalRecord",totalRecord);
        map.put("keyWord",searchKeyword);
        int sumPage = fillRepairResult_materialImp.getRepairMaterialPageSum(searchKeyword);
        map.put("sumPage", new Integer(sumPage));
        List<Repairtaskorder> taskList = fillRepairResult_materialImp.getTaskMaterialListByLimit(curPage, searchKeyword);
        map.put("taskList",taskList);

        return map;
    }

    @RequestMapping("/getTaskResultList")
    @ResponseBody
    public Map<String, Object> getTaskResultList(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        int curPage = Integer.parseInt(request.getParameter("curPage"));
        int totalRecord = customedFillRepairResult_Material.getResultNumber();

        map.put("totalRecord",totalRecord);
        int sumPage = fillRepairResult_materialImp.getRepairResultPageSum();
        map.put("sumPage", new Integer(sumPage));
        List<Repairtaskorder> taskList = fillRepairResult_materialImp.getRepairResultListByLimit(curPage);
        map.put("taskList",taskList);

        return map;
    }

}
